package com.example.helloopencv;



import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.WindowManager;

public class MainActivity extends Activity implements CvCameraViewListener2{

	 private CameraBridgeViewBase mOpenCvCameraView;

	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	    // Log.i(TAG, "called onCreate");
	     super.onCreate(savedInstanceState);
	     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	     setContentView(R.layout.activity_main);
	     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
	     mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
	     mOpenCvCameraView.setCvCameraViewListener(this);
	 }

	 @Override
	 public void onPause()
	 {
	     super.onPause();
	     if (mOpenCvCameraView != null)
	         mOpenCvCameraView.disableView();
	 }

	 public void onDestroy() {
	     super.onDestroy();
	     if (mOpenCvCameraView != null)
	         mOpenCvCameraView.disableView();
	 }
	 Mat mRgba;

	 public void onCameraViewStarted(int width, int height) {
		 mRgba = new Mat(height, width, CvType.CV_8UC4);
	 }

	 public void onCameraViewStopped() {
	 }

	 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
		 mRgba = inputFrame.rgba();
		 int height = mRgba.height();
		 int width = mRgba.width();
	        Mat mHsv = new Mat(height, width, CvType.CV_8UC4);
	        Mat mTemp1 = new Mat(height, width, CvType.CV_8UC4);
	        Imgproc.cvtColor(mRgba,mHsv,Imgproc.COLOR_RGB2HSV);
	        //Check for red
	       Core.inRange(mHsv, new Scalar(110, 70, 30), new Scalar(113, 255, 255), mTemp1);
	     //   Core.inRange(mHsv, new Scalar(0, 100, 30), new Scalar(5, 255, 255), mTemp1);
	        Mat m = new Mat(height, width, CvType.CV_8UC4);
	        Core.extractChannel(mTemp1, m, 0);
	        int n = Core.countNonZero(m);
	        System.out.println("Value of n: " +  n);
	     return inputFrame.rgba();
	 }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
	    @Override
	    public void onManagerConnected(int status) {
	        switch (status) {
	            case LoaderCallbackInterface.SUCCESS:
	            {
	             
	                mOpenCvCameraView.enableView();
	            } break;
	            default:
	            {
	                super.onManagerConnected(status);
	            } break;
	        }
	    }
	};

	@Override
	public void onResume()
	{
	    super.onResume();
	    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, mLoaderCallback);
	}

}
